package br.com.rotaestatistica.daos;

import br.com.rotaestatistica.entities.*;
import br.com.rotaestatistica.utils.FabricaDeConexao;

import java.util.ArrayList;
import java.util.List;
import java.sql.*;

import javax.faces.model.SelectItem;

public class ClimaDAO extends FactoryDAO {

	public List<ClimaEntity> selectAllClimasAsList() {
		List<ClimaEntity> listaRetorno = new ArrayList<ClimaEntity>();
		ResultSet rs=null;
		Statement stm=null;
		try{
		stm = FabricaDeConexao.getConexao().createStatement();
		
		String sql = "SELECT * FROM Clima";
		
		rs = stm.executeQuery(sql);
		
		while (rs.next()){
			ClimaEntity clima = new ClimaEntity();
			clima.setId_Clima(rs.getLong ("idclima"));
			clima.setNome(rs.getString ("nome"));
			clima.setDescricao(rs.getString("descricao"));
			
			listaRetorno.add(clima);
		}
		}
		catch (SQLException e){
			e.printStackTrace();
		}
		finally{
			try{
				rs.close();
				stm.close();
				FabricaDeConexao.fechaConexao();
			}
			catch (SQLException e){
				e.printStackTrace();
			}
		}
		return listaRetorno;
	}

	public ClimaEntity selectClimaById(long idClima) {
		ClimaEntity clima = new ClimaEntity();
		ResultSet rs = null;
		PreparedStatement pstm = null;
		
		String SQL = "SELECT * FROM Clima where id_Clima =?";
		try{
			
		pstm = FabricaDeConexao.getConexao().prepareStatement(SQL);
		
		rs = pstm.executeQuery(SQL);
		
		while (rs.next()){
			clima.setId_Clima(rs.getLong ("idClima"));
			clima.setNome(rs.getString ("nome"));
			clima.setDescricao(rs.getString("descricao"));
		}
		}
		catch (SQLException e){
			e.printStackTrace();
		}
		finally{
			try{
				rs.close();
				pstm.close();
				FabricaDeConexao.fechaConexao();
			}
			catch (SQLException e){
				e.printStackTrace();
			}
		}
		
		return clima;
	}

	public boolean insertClima(ClimaEntity clima) {
		PreparedStatement pstm=null;
		
		String sql = "INSERT INTO Clima (nome, descricao) VALUES ('?','?')";
		
		try{
		pstm = FabricaDeConexao.getConexao().prepareStatement(sql);
		pstm.setString(1, clima.getNome());
		pstm.setString(2, clima.getDescricao());		 
		
		pstm.executeUpdate(sql);
		return true;
		}
		catch (SQLException e){
			e.printStackTrace();
			return false;
		}
		finally{
			try{
				pstm.close();
				FabricaDeConexao.fechaConexao();
			}
			catch (SQLException e){
				e.printStackTrace();
			}
		}
	}

	public boolean deleteClimaById(long idClima) {
		PreparedStatement pstm = null;
		
		String sql = "DELETE FROM Clima where id_Clima =?";
		
		try{
		pstm = FabricaDeConexao.getConexao().prepareStatement(sql);
		pstm.setLong(1, idClima);
		
		pstm.executeUpdate(sql);
		
		return true;
		}
		catch (SQLException e){
			e.printStackTrace();
			return false;
		}
		finally{
			try{
				pstm.close();
				FabricaDeConexao.fechaConexao();
			}
			catch (SQLException e){
				e.printStackTrace();
			}
		}
		
	}

	public boolean updateClima(ClimaEntity clima) { 
		PreparedStatement pstm = null;
		
		String sql = "UPDATE Clima SET nome =?, descricao =? WHERE id_Clima =?";
		try{
		pstm = FabricaDeConexao.getConexao().prepareStatement(sql);
		pstm.setString(1, clima.getNome());
		pstm.setString(2, clima.getDescricao());
		pstm.setLong(3, clima.getId_Clima());
		
		pstm.executeUpdate(sql);
		return true;
		}
		catch (SQLException e){
			e.printStackTrace();
			return false;
		}
		finally{
			try{
				pstm.close();
				FabricaDeConexao.fechaConexao();
			}
			catch (SQLException e){
				e.printStackTrace();
			}
		}
	}

	public List<SelectItem> getAllClimasAsSelectItems() {
		
		List<ClimaEntity> climas = new ArrayList<ClimaEntity>();
		climas = selectAllClimasAsList();
		
		List<SelectItem> climasAsSelectItems = new ArrayList<SelectItem>();
		
		for (ClimaEntity clima : climas) {
			climasAsSelectItems.add(new SelectItem(clima.getId_Clima(), clima.getNome()));
		}
		
		return climasAsSelectItems;
	}
}
